package com.wx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.wx.entity.WxMulReply;
import com.wx.entity.WxMulReplyExample;
import com.wx.entity.WxPicReply;
import com.wx.entity.WxPicReplyExample;
import com.wx.service.WxMulReplyService;
import com.wx.service.WxPicReplyService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.common.util.Page;
import com.zyht.website.entity.SysUser;

@Controller
@RequestMapping("admin/mul")
public class WxMulReplyController extends AbsManageBaseController{
	@Resource
	private WxMulReplyService wxMulReplyService;
	@Resource
	private WxPicReplyService wxPicReplyService;
	
	@Value("#{propertiesReader[session_user_key]}")
	private String sessionKey;
	@Value("#{propertiesReader[session_wx_key]}")
	private String session_wx_key;
	@RequestMapping("index")
	public String index(){
		return "basic/news/more_add";
	}
	
	@RequestMapping("list")
	public String list(Integer currentPage, Model model, HttpServletRequest request){
		String wxid = getWxId(request);
		WxMulReplyExample example = new WxMulReplyExample();
		example.createCriteria().andWxidEqualTo(wxid);
		
		int total = this.wxMulReplyService.countByExample(example);
		if(null==currentPage){
			currentPage = 1;
		}
		String pageSizeStr = PropertiesUtils.getConfig("paganation_page_size");
		int pageSize = Integer.parseInt(pageSizeStr);
		
		example.setOrderByClause("id desc");
		PageHelper.startPage(currentPage, pageSize);
		List<WxMulReply> list = this.wxMulReplyService.selectByExample(example);
		
		//组装单个图文回复
		List<Integer> values = new ArrayList<Integer>();
		for(WxMulReply bean : list){
			String pics = bean.getPicIds();
			String[] picArray = pics.split(",");
			for(String pic : picArray){
				if(StringUtils.isEmpty(pic)){
					continue;
				}
				values.add(Integer.valueOf(pic));
			}
		}
		if(values.size()>0){
			WxPicReplyExample pe = new WxPicReplyExample();
			pe.createCriteria().andWxidEqualTo(wxid).andIdIn(values);
			List<WxPicReply> replys = this.wxPicReplyService.selectByExample(pe);
			Map<Integer, WxPicReply> map = new HashMap<Integer, WxPicReply>();
			for(WxPicReply reply : replys){
				map.put(reply.getId(), reply);
			}
			
			for(WxMulReply bean : list){
				String pics = bean.getPicIds();
				String[] picArray = pics.split(",");
				for(String pic : picArray){
					if(StringUtils.isEmpty(pic)){
						continue;
					}
					bean.getPics().add(map.get(Integer.valueOf(pic)));
				}
			}
		}
		
		Page<WxMulReply> page = new Page<WxMulReply>(total, currentPage, pageSize);
		page.setRecordList(list);
		model.addAttribute("page", page);
		return "basic/news/more_list";
	}
	
	@RequestMapping("persis")
	public String save(WxMulReply bean, Model model, HttpServletRequest request){
		model.addAttribute("redirect", "admin/mul/index");
		if(StringUtils.isEmpty(bean.getKeyword())){
			model.addAttribute("msg", "关键词不能为空");
			return "message";
		}
		if(StringUtils.isEmpty(bean.getPicIds())){
			model.addAttribute("msg", "请选择图文消息");
			return "message";
		}
		String wxid = getWxId(request);
		bean.setWxid(wxid);
		SysUser sysUser = getSessionUser(request);
		if (bean.getId() == null) {
			bean.setUid(sysUser.getId());
		}
		this.wxMulReplyService.insert(bean);
		return "message";
	}

	@RequestMapping("del")
	public String delelte(Integer id, Model model, HttpServletRequest request){
		WxMulReplyExample example = new WxMulReplyExample();
		example.createCriteria().andIdEqualTo(id);
		this.wxMulReplyService.deleteByPrimaryKey(example);
		model.addAttribute("redirect", "admin/mul/list");
		return "message";
	}
}
